package com.mindskip.xzs.controller.admin;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;

import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.domain.Video;
import com.mindskip.xzs.domain.VideoUser;
import com.mindskip.xzs.service.UserService;
import com.mindskip.xzs.service.VideoService;
import com.mindskip.xzs.service.VideoUserService;
import com.mindskip.xzs.viewmodel.admin.education.VideoPageRequestVM;
import com.mindskip.xzs.viewmodel.admin.education.VideoUserPageRequestVM;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author changjiang
 * @Title:
 * @Package
 * @Description: 视频添加
 * @date 2022/1/263:13 下午
 */
@RestController("AdminVideoController")
@RequestMapping(value = "/api/admin/video")
public class VideoController extends BaseApiController {

    private final VideoService videoService;
    private final UserService userService;
    private final VideoUserService videoUserService;


    public VideoController(VideoService videoService,UserService userService,VideoUserService videoUserService) {
        this.videoService = videoService;
        this.userService = userService;
        this.videoUserService = videoUserService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public RestResponse<List<Video>> pageList(@RequestBody VideoPageRequestVM model) {
        String videoName = model.getVideoName();
        if(StringUtils.isEmpty(videoName) || "".equals(videoName)){
            model.setVideoName(null);
        }
        List<Video> pageInfo = videoService.videoPage(model);
        return RestResponse.ok(pageInfo);
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<Video>> pageList2(@RequestBody VideoPageRequestVM model) {
        String videoName = model.getVideoName();
        if(StringUtils.isEmpty(videoName) || "".equals(videoName)){
            model.setVideoName(null);
        }
        if(StringUtils.isEmpty(model.getRealName())){
            model.setRealName(null);
        }
        if(null == model.getVideoType()){
            model.setVideoType(0);
        }
        PageInfo<Video> pageInfo = videoService.pageList(model);
        //视频批阅
        if(1== model.getVideoType()){
            pageInfo = videoService.page2(model);
            List<Video> videoList = pageInfo.getList();
            for(Video video : videoList){
                Integer createUserId = video.getCreateUserId();
                int isCheck = video.getIsCheck();
                if(isCheck == 0){
                    video.setIsPass(2);
                }
                if(null == createUserId){
                    continue;
                }
                //根据用户id查询用户信息
                User user = userService.getUserById(createUserId);
                if(null == user){
                    continue;
                }
                video.setVideoName("实操视频");
                video.setIdCard(user.getIdCardNo());
                video.setRealName(user.getRealName());
                video.setPhone(user.getPhone());
                video.setUserName(user.getUserName());
                video.setUserLevel(user.getUserLevel());
                List<Video> children = videoService.videoPage2List(video);
                for(Video childrenVideo : children){
                    User user2 = userService.getUserById(createUserId);
                    if(null == user2){
                        continue;
                    }
                    childrenVideo.setIdCard(user2.getIdCardNo());
                    childrenVideo.setRealName(user2.getRealName());
                    childrenVideo.setPhone(user2.getPhone());
                    childrenVideo.setUserName(user2.getUserName());
                    childrenVideo.setUserLevel(user2.getUserLevel());
                }
                video.setChildren(children);
            }
            pageInfo.setList(videoList);
        }

        return RestResponse.ok(pageInfo);
    }

    @RequestMapping(value = "/page2", method = RequestMethod.POST)
    public RestResponse<PageInfo<Video>> pageList3(@RequestBody VideoPageRequestVM model) {
        String videoName = model.getVideoName();
        if(StringUtils.isEmpty(videoName) || "".equals(videoName)){
            model.setVideoName(null);
        }
        model.setVideoType(1);
        PageInfo<Video> pageInfo = videoService.page(model);
        return RestResponse.ok(pageInfo);
    }


    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid Video model) {
        int vm = videoService.updateById(model);
        return RestResponse.ok(vm);
    }


    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<Video> select(@PathVariable Integer id) {
        Video vm = videoService.selectById(id);
        return RestResponse.ok(vm);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        Video video = videoService.selectById(id);
        video.setState("1");
        videoService.updateByIdFilter(video);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public RestResponse deleteById(@RequestBody @Valid Video model) {
        model.setDeleted(1);
        videoService.updateByIdFilter(model);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RestResponse add(@RequestBody @Valid Video model) {
        model.setUpdateTime(new Date());
        int vm = 0;
        if (null == model.getId()) {
            model.setCreateTime(new Date());
            vm =videoService.insertSelective(model);
        } else {
            if(null != model.getIsPass()){
                model.setIsCheck(1);
            }
            vm= videoService.updateByPrimaryKeySelective(model);
        }
        return RestResponse.ok(vm);
    }


    @RequestMapping(value = "/rate/list", method = RequestMethod.POST)
    public RestResponse<PageInfo<VideoUser>> watchRateList(@RequestBody VideoUserPageRequestVM model) {
        if(StringUtils.isEmpty(model.getPhone())){
            model.setPhone(null);
        }
        if(StringUtils.isEmpty(model.getRealName())){
            model.setRealName(null);
        }
        PageInfo<VideoUser> pageInfo = videoUserService.getVideoUserListPage(model);
        List<VideoUser> userList = pageInfo.getList();
        if(CollUtil.isNotEmpty(userList)) {
            for(VideoUser videoUser : userList){
                videoUser.setVideoName("学习课程");
                videoUser.setVideoUrl("");
                Integer countTime = videoUserService.countTime(videoUser);
                videoUser.setLearnSumTime(String.valueOf(countTime));
                videoUser.setVideoWatch(String.valueOf(countTime));
                Integer countVideoTime = videoUserService.countTimeVideo(videoUser);
                videoUser.setVideoTime(String.valueOf(countVideoTime));
                String videoRate = getRate(videoUser.getVideoWatch(),videoUser.getVideoTime());
                videoUser.setVideoRate(videoRate);
                List<VideoUser> children = videoUserService.getVideoUserListForUid(videoUser);
                videoUser.setChildren(children);
            }
        }
        pageInfo.setList(userList);

        //加入其他字段
        return RestResponse.ok(pageInfo);
    }


    private String getRate(String videoWatch,String videoTime){
        java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance();
        //设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        Float videoTotal = Float.parseFloat(videoTime);
        Float videoNow = Float.parseFloat(videoWatch);
        String rate1 = numberFormat.format((videoNow / videoTotal) * 100);
        if (StringUtils.isEmpty(rate1) || "0".equals(rate1) || videoTotal.equals(0.00)) {
            rate1 = "1";
        }
        if ("0".equals(videoTime)) {
            rate1 = "0";
        }
        String rate = rate1 + "%";
        return rate;
    }


}
